Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

207
Visualizações
Phaser 3 - Move gameObject to mouse click ("click to move" movement") using tween

So, I'm making a game with Phaser 3 that will need a "click to move" movement, but I can't use physics so I want to change it and use a tween to make the player move to the clicked position ("click to move"). This was the code that I was using that used physics:

var target = new Phaser.Math.Vector2();

preload(){
    this.load.image("image", "assets/image.png");
}

create(){
    player = this.physics.add.sprite(400, 350, "image");

    this.input.on('pointerup', function (pointer) {

        target.x = pointer.x;
        target.y = pointer.y;
        
        // Move at 200 px/s:
        this.physics.moveToObject(player, target, 200);

    }, this);
}

update(){
    var distance = Phaser.Math.Distance.Between(player.x, player.y, target.x, target.y);

    if (player.body.speed > 0)
    {
        //  4 is our distance tolerance. The faster it moves, the more tolerance is required.
        if (distance < 4)
        {
            player.body.reset(target.x, target.y);
            player.anims.stop();
        }
    }
}

I tried doing it (using tween), and it works but not as I expected, the results are a lot different from the first code that used physics, it moves much faster and the object (player) keeps teletransporting to the tween's original x and y. Here is what I tried:

var tween;
var target = new Phaser.Math.Vector2();

preload(){
    this.load.image("image", "assets/image.png");
}

create(){
    player = this.add.sprite(400, 300, "image");

    tween = this.tweens.add({
        targets: player,
        x: 400,
        y: 300,
        ease: 'Power1',
        duration: -1,
        paused: true
    });

    this.input.on('pointerdown', function (pointer) {

        target.x = pointer.x
        target.y = pointer.y

        tween.play();
    });
}

update(){
    if (tween.isPlaying())
    {
        tween.updateTo('x', target.x, true);
        tween.updateTo('y', target.y, true);
    }
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda